package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.domain.RechargeOffline;
import com.xmg.p2p.business.query.RechargeOfflineQueryObject;
import com.xmg.p2p.business.service.IPlatformBankinfoService;
import com.xmg.p2p.business.service.IRechargeOfflineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @author: Andy
 * @date: 2021.4.8 11:13
 * @Description: 前台充值相关的控制器
 * @since JDK 1.8
 */
@Controller
public class RechargeOfflineController {
    @Autowired
    private IPlatformBankinfoService platformBankinfoService;
    @Autowired
    private IRechargeOfflineService rechargeOfflineService;

    /**
     * 导向线下充值页面
     *
     * @param model
     * @return
     */
    @RequireLogin
    @RequestMapping("recharge")
    public String recharge(Model model) {
        model.addAttribute("banks", this.platformBankinfoService.listAll());
        return "recharge";
    }

    /**
     * 前台申请一个充值单
     * @param ro
     * @return
     */
    @RequestMapping("recharge_save")
    @ResponseBody
    public JSONResult rechargeApply(RechargeOffline ro) {
        JSONResult json = new JSONResult();
        try {
            this.rechargeOfflineService.apply(ro);
        } catch (Exception e) {
            e.printStackTrace();
            json.setSuccess(false);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     * 用户查看充值明细
     *
     * @param qo
     * @param model
     * @return
     */
    @RequestMapping("recharge_list")
    public String recharge(@ModelAttribute("qo")
                                   RechargeOfflineQueryObject qo, Model model) {
        qo.setApplierId(UserContext.getCurrent().getId(
        )); //设置只允许查自己的记录
        model.addAttribute("pageResult", this.rechargeOfflineService.query(qo));
        return "recharge_list";
    }

}
